//声明变量
var express = require('express');
var app = express();
var url = require("url");
//参数转化
var bodyParser = require('body-parser');
app.use(bodyParser.text());
app.use(express.static('public'));
//数据库链接
var mysql = require('mysql');
var db = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'root',
  password : 'tobe321',
  database : 'xyb'
});
db.connect();

var uName;
var psw;
//注册请求
app.post('/register',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);

    uName = '"'+ body.uName +'"';
    psw = '"'+ body.psw +'"';
    //查找数据库
    var login='select * from users where uName = '+ uName;
    db.query(login,function(error, results){
        if(error) throw error;
        console.log(results);
        //用户不存在
        if(results.length == 0){
            var message='insert into users(uName,psw) values (' + uName + ',' + psw + ");";
            db.query(message,function(error,results){
            if(error) throw error;
            console.log(uName);
            console.log('注册成功');
            });
            res.send('注册成功');
        }
        //用户已存在
        else{
            console.log('用户已存在');
            res.send('用户已存在');
        }
    })
});

//登录请求
app.post('/login',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);

    uName = '"'+ body.uName +'"';
    psw = '"'+ body.psw +'"';
    //查找数据库
    var login='select * from users where uName = '+ uName;
    db.query(login,function(error, results){
        if(error) throw error;
        console.log(results);
        //用户不存在
        if(results.length == 0){
            console.log('用户不存在');
            res.send('用户不存在');
        }
        //用户存在
        else{
            if(results[0].psw == body.psw){
                console.log('登录成功');
                res.send('登录成功'+results[0].uName)
            }
            else{
                console.log('密码错误');
                res.send('密码错误')
            }
        }
    })
});

//获取个人信息
app.post('/getUserMsg',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);
    
    uName = '"'+ body.uName +'"';
    //查找数据库
    var query = 'select * from users where uName = '+uName;
    db.query(query,function(error, results){
        if(error) throw error;
        console.log(results);
        if(results.length==0){
            console.log('没有数据');
            res.send('没有数据');
        }else{
            console.log("获取成功");
            res.send(results);
        }
    })
})

//上传商品信息
app.post('/getgoods',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");//跨域处理
    let body = JSON.parse(req.body);

    goods_name = '"'+ body.goods_name +'"';
    goods_num = '"'+ body.goods_num +'"';
    goods_desc = '"'+ body.goods_desc +'"';
    goods_info = '"'+ body.goods_info +'"';
    goods_price = '"'+ body.goods_price +'"';
    goods_picture = '"'+ body.goods_picture +'"';
    goods_master = '"'+ body.goods_master +'"'

    var dealgoods = 'insert into goods(goods_name,goods_num,goods_desc,goods_info,goods_price,goods_picture,goods_master) values (' + goods_name + ',' +goods_num+','+ goods_desc + ',' + goods_info + ','+ goods_price + ','+ goods_picture +','+goods_master+");";
    db.query(dealgoods,function(error, results){
    if(error) throw error;
    if(results.length==0){
        console.log('没有数据');
        res.send('没有数据');
    }else{
    console.log("提交成功");
    res.send(results);
    }
      })
})

//获取市场商品信息
app.post('/Goods',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body); 
    //查找数据库
    var query = 'select * from goods ';
    db.query(query,function(error, results){
        if(error) throw error;
        console.log(results);
        if(results.length==0){
            console.log('没有数据');
            res.send('没有数据');
        }else{
            console.log("获取成功");
            res.send(results);
        }
    })
})

//我的发布
app.post('/Mygoods',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body); 

    goods_master = '"'+ body.goods_master +'"'
    //查找数据库
    var query1 = 'select * from goods where goods_master ='+ goods_master;
    db.query(query1,function(error, results){
        if(error) throw error;
        console.log(results);
        if(results.length==0){
            console.log('没有数据');
            res.send('没有数据');
        }else{
            console.log("获取成功");
            res.send(results);
        }
    })
})

//搜索
app.post('/Value',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body); 

    value = '"'+ body.value +'"'
    //查找数据库
    var query = 'select * from goods where instr('+'goods_name'+','+value+')';
    db.query(query,function(error, results){
        if(error) throw error;
        console.log(results);
        if(results.length==0){
            console.log('没有数据');
            res.send('没有数据');
        }else{
            console.log("获取成功");
            res.send(results);
        }
    })
})

//修改个人信息
app.post('/ResetUser',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);
    
    uName = '"'+ body.uName +'"';
    name ='"'+ body.name +'"';
    stuID = '"'+ body.stuID +'"';
    phoneNum = '"'+ body.phoneNum +'"';
    IDcard = '"'+ body.IDcard +'"'
    //设置数据库
    var userMsg = 'update users set name = '+name+ ','+
                  'stuID = '+stuID+ ','+
                  'phoneNum = '+phoneNum+ ','+
                  'IDcard = '+IDcard+ " where uName = "+uName
    db.query(userMsg,function(error, results){
        if(error) throw error;
        console.log(results);
        res.send(results);
    })
})

//忘记密码=>修改密码
app.post('/ResetPsw',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);
    
    newPsw = '"'+ body.newPsw +'"'
    uName = '"'+ body.uName +'"';
    //设置数据库
    var pswMsg = 'update users set psw = '+newPsw+" where uName = "+uName
    db.query(pswMsg,function(error, results){
        if(error) throw error;
        console.log(results);
        res.send(results);
    })
})

//收藏请求
app.post('/Clgoods',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);

    uName = '"'+ body.clName +'"';
    cltitle='"'+ body.clg_title +'"';   
    cldesc='"'+ body.clg_desc +'"';   
    clprice='"'+ body.clg_price +'"';  
    climg='"'+ body.clg_img +'"';  
    clinfo='"'+ body.clg_info +'"';  
    //查找数据库
    var collection='select * from clgoods where clName = '+ uName +'and clg_title =' + cltitle;
    db.query(collection,function(error, results){
        if(error) throw error;
        console.log(results);
        //未收藏
        if(results.length == 0){
            var clmessage='insert into clgoods(clName,clg_title,clg_desc,clg_price,clg_img,clg_info) values (' + uName + ',' + cltitle + ',' + cldesc + ',' + clprice + ',' + climg + ',' + clinfo + ");";
            db.query(clmessage,function(error,results){
            if(error) throw error;
            console.log(uName);
            console.log('收藏成功');
            });
            res.send('收藏成功');
           
        }
        //已收藏
        else{
            console.log('请勿重复收藏');
            res.send('请勿重复收藏');
        }
    })
});

//取消收藏
app.post('/cancel_Usercl',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);
    
    uName = '"'+ body.uName +'"';
    clg_title='"'+ body.clGoodss.clg_title +'"';
    //设置数据库
    var clgood = 'delete from clgoods where clName = '+uName;
    db.query(clgood,function(error, results){
        if(error) throw error;
        console.log(results);
        if(results.length==0){
            console.log('取消成功');
            res.send(null);
        }else{
            res.send("系米都成功");
        }
        
    })
})

//获取用户收藏信息
app.post('/getUsercl',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);
    
    uName = '"'+ body.uName +'"';
    //查找数据库
    var clgood = 'select * from clgoods where clName = '+uName;
    db.query(clgood,function(error, results){
        if(error) throw error;
        console.log(results);
        if(results.length==0){
            console.log('没有数据');
            res.send(null);
        }else{
            console.log("获取成功");
            res.send(results);
        }
    })
})

//发送评论
app.post('/sendComment',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);
    
    uName = '"'+ body.sendUser +'"';
    content ='"'+ body.content +'"';
    goodsname ='"'+ body.goodsname +'"';
  
    //存入数据库
    var comMsg  = 'insert into comment(comment_name,comment_content,comment_user) values (' + goodsname+ ',' + content + ','+ uName +");";
    db.query(comMsg,function(error, results){
        if(error) throw error;
        console.log(results);
        console.log('评论成功');
        res.send('评论成功');
    })
})

//获取商品评论信息
app.post('/getComment',(req,res)=>{
    res.header("Access-Control-Allow-Origin", "*");
    let body = JSON.parse(req.body);  
    gc='"'+ body.getcom +'"';
    //查找数据库
    var goodc = 'select * from comment where comment_name ='+ gc;
    db.query(goodc,function(error, results){
        if(error) throw error;
        console.log(results);
        if(results.length==0){
            console.log('没有评论数据');
        }else{
            console.log("获取成功");
            res.send(results);
        }
    })
})

app.listen(3000,()=>{
    console.log('server listen 3000');
  });